/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Himanshu  Sharma
 */

import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.util.Random ;



public class Impulse implements Runnable {
    JFrame f1, f2 ;
    JLabel l1,l2,header, l3, l4, inst ;
    JLabel[] coins;
    JButton b1 ;
    JPanel p1, timeDisplay, moneyDisplay ;
    long lastTime, startTime;
    int time;
    double money, money_won ;
    long currTime ;
    String heads, tails;
    boolean[] setcoins; // = {false, false, false, false, false} ;
    Thread t1 ;
    int c = 0;
    Game G;
    int numOfCoins = 0, numOfHeads = 0;
    Icon a;

    Impulse(){
         this.money = 100.0;
         this.numOfCoins = 10;
         this.time = 1000;
         InitComponents(); 
    };

    Impulse( int numOfCoins, double money, int time  ) {
         this.money = money; 
         this.numOfCoins = numOfCoins; 
         this.time = time; 
         InitComponents(); 
   }

    public static void main(String [] args) {
        Impulse i = new Impulse();

    }

    public void Flip(){
        
        Random r = new Random() ;

        for (int i=0; i<setcoins.length; i++){
            boolean prevCoin = setcoins[i];
            setcoins[i] = r.nextBoolean();
            if(prevCoin==false && setcoins[i]==true){
                coins[i].setIcon(new ImageIcon("tails2heads.gif"));
                coins[i].repaint();
            }
            else if(prevCoin==true && setcoins[i]==false)
            {
                coins[i].setIcon(new ImageIcon("heads2tails.gif"));
                coins[i].repaint();
            }
            else if(prevCoin==true && setcoins[i]==true)
            {
                coins[i].setIcon(new ImageIcon("heads.jpg"));
                coins[i].repaint();

            }
            else if ( prevCoin==false && setcoins[i]==false )
            {
                coins[i].setIcon(new ImageIcon("tails.jpg"));
                coins[i].repaint();

            }

        }

    }



    public void InitComponents() {
        f1= new JFrame("Impulse") ;
        G = new Game();
        p1 = new JPanel() ;
        p1.setBackground(Color.white) ;
        timeDisplay = new JPanel() ;
        moneyDisplay = new JPanel() ;
        l1 = new JLabel() ;
        l2 = new JLabel() ;
        l3 = new JLabel() ;
        l4 = new JLabel() ;
        header = new JLabel("Impulse") ;
        b1= new JButton("Stop") ;
        b1.addActionListener(new ButtonListener()) ;
        f1.addMouseListener(new MouseInput()) ;
        coins = new JLabel[numOfCoins] ;
        setcoins = new boolean[numOfCoins];
        
        heads = "Heads" ;
        tails = "Tails" ;
        t1 = new Thread(this, "impulse") ;

        f1.getContentPane().add(BorderLayout.SOUTH, b1) ;
        f1.getContentPane().add(BorderLayout.CENTER, p1) ;
        f1.getContentPane().add(BorderLayout.WEST, timeDisplay) ;
        f1.getContentPane().add(BorderLayout.EAST, moneyDisplay) ;
        f1.setBackground(Color.white);
        f1.setForeground(Color.white);
        f1.getContentPane().add(BorderLayout.NORTH, header) ;

        l3.setText("Time Remaining") ;
        l4.setText("Money Remaining") ;
        timeDisplay.add(l3);
        timeDisplay.add(l1) ;
        moneyDisplay.add(l4) ;
        moneyDisplay.add(l2) ;

        f1.setSize(1000, 500);
        f1.setVisible(true);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;




        for(int i = 0; i < setcoins.length; i++) {
            coins[i] = new JLabel() ;
            Random k = new Random() ;
            setcoins[i] = k.nextBoolean() ;
            if(setcoins[i]==true){
                //coins[i].setText(heads) ;
                coins[i].setIcon(new ImageIcon("heads.jpg"));
            }
            else{
                coins[i].setIcon(new ImageIcon("tails.jpg"));
            }
            p1.add(coins[i]) ;
        }

        l1.setText(Integer.toString(time));
        l2.setText(Double.toString(money));

         t1.start();



    }

    public synchronized void run()
    {
        int counter;
        counter = 0 ;
        startTime = System.currentTimeMillis() ;
        lastTime = System.currentTimeMillis() ;
        l1.setText(Integer.toString(time)) ;
        l2.setText(Double.toString(money)) ;
        
        

        while (time !=0 && t1 != null) {
            currTime = System.currentTimeMillis() ;
            if( (currTime-lastTime) >= 1000){
                time = time -1;
                money = money - .10 ;
                lastTime = currTime ;
                l1.setText(Integer.toString(time));
                l2.setText(String.format("%.2f", money)) ;
                counter = counter + 1 ;
                if (counter == 3){
                    Flip() ;
                    counter = 0 ;
                }
            }else{
                try{
                    Thread.sleep(1000);
                    continue ; 
                }catch( InterruptedException ex ){
                    ex.printStackTrace();
                }
            }
            
        }

    }


    class MouseInput implements MouseListener{
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e){
            if(c==0){
                t1 = null ;
                b1.setText("Next");

                for (int i = 0; i < setcoins.length ; i++ ){
                    if(setcoins[i] == true) {
                        numOfHeads++;
                    }
                }

                if(numOfHeads == setcoins.length) {
                    money_won = 10;
                }
                else
                {
                    money_won = money;
                }
                header.setText(String.format("%.2f", money_won)) ;
                c++;
                try{

                    G.writeLog("Impulse: Money_won $" + String.format("%.2f", money_won) + " Time_taken: " + (lastTime - startTime)/1000 + "sec");
                }catch(Exception fileExcep){
                    System.out.println("Error writing to the file...Impulse " + fileExcep);
                }

            }
            else{
                f1.setVisible(false);
                G.free = true;

            }
        }
    }

    class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            if(c==0){
                t1 = null ;
                b1.setText("Next");

                for (int i = 0; i < setcoins.length ; i++ ){
                    if(setcoins[i] == true) {
                        numOfHeads++;
                    }
                }

                if(numOfHeads == setcoins.length) {
                    money_won = 10;
                }
                else
                {
                    money_won = money;
                }
                header.setText(String.format("%.2f", money_won)) ;
                c++;
                try{
                   
                    G.writeLog("Impulse: Money_won $" + String.format("%.2f", money_won) + " Time_taken: " + (lastTime - startTime)/1000 + "sec");
                }catch(Exception fileExcep){
                    System.out.println("Error writing to the file...Impulse " + fileExcep);
                }

            }
            else{
                f1.setVisible(false);
                G.free = true; 
            }
        }
    }


}
